//
//  TWScrollLabelController.m
//  特效动画
//
//  Created by 耐克了解了 on 1/6/17.
//  Copyright © 2017年 耐克了解了. All rights reserved.
//

#import "TWScrollLabelController.h"
#import "TWScrollLabelView.h"
#import "NSString+XHHExtension.h"

@interface TWScrollLabelController ()<TWScrollLabelViewDelegate>

@end

@implementation TWScrollLabelController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    TWScrollLabelView *barrageView0 = [[TWScrollLabelView alloc] initWithFrame:CGRectMake(0, 104, self.view.frame.size.width, 20)];
    TWScrollLabelView *barrageView1 = [[TWScrollLabelView alloc] initWithFrame:CGRectMake(0, 124, self.view.frame.size.width, 20)];
    TWScrollLabelView *barrageView2 = [[TWScrollLabelView alloc] initWithFrame:CGRectMake(0, 144, self.view.frame.size.width, 20)];
    
    
    barrageView0.delegate = self;
    barrageView1.delegate = self;
    barrageView2.delegate = self;
    
    [self.view addSubview:barrageView0];
    [self.view addSubview:barrageView1];
    [self.view addSubview:barrageView2];
    
    [barrageView0 startAnimation];
    [barrageView1 startAnimation];
    [barrageView2 startAnimation];
}

#pragma mark -
- (UILabel *)createLabelWithText:(NSString *)text textColor:(UIColor *)textColor {
    
    NSString *string = [NSString stringWithFormat:@" %@ ", text];
    CGFloat width = [string widthWithStringAttribute:@{NSFontAttributeName : [UIFont systemFontOfSize:14.f]}];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 20)];
    label.font = [UIFont systemFontOfSize:14.f];
    label.text = string;
    label.textColor = textColor;
    return label;
}

#pragma mark - TWScrollLabelViewDelegate

- (void)barrageView:(TWScrollLabelView *)barrageView animationDidStopFinished:(BOOL)finished {
    
    [barrageView stopAnimation];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [barrageView addContentView:[self createLabelWithText:[self randomString]
                                                    textColor:[self randomColor]]];
        [barrageView startAnimation];
    });
}


- (NSString *)randomString {
    
    NSArray *array = @[@"猜猜我是谁?",
                       @"哈哈😁",
                       @"猜不着吧",
                       @"我是程序媛",
                       @"噜啦啦啦啦~"];
    return array[arc4random() % array.count];
}

#pragma mark - 产生随机色

- (UIColor *)randomColor {
    
    return [UIColor colorWithRed:arc4random() % 256 / 255.f green:arc4random() % 256 / 255.f blue:arc4random() % 256 / 255.f alpha:1];
}
@end
